﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using SQLExecute;


public partial class list_major_subject : System.Web.UI.Page
{
    
    school s = new school();

    MajorSubject m = new MajorSubject();
    static int nMajorSubjectID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null && Session["fullname"] != null)
            {
                Init_Combo();
                BindGrid();
                lUsername.Text = " " + Session["username"].ToString();
            }
            else
                Response.Redirect("login/login.aspx");
        }
    }

    private void Init_Combo()
    {
        cbCity.Items.Insert(0, new ListItem("Ho Chi Minh", "HCM"));
        cbCity.Items.Insert(1, new ListItem("Ha Noi", "HN"));
        cbCity.SelectedIndex = 0;

        cbCity2.Items.Insert(0, new ListItem("Ho Chi Minh", "HCM"));
        cbCity2.Items.Insert(1, new ListItem("Ha Noi", "HN"));
        cbCity2.SelectedIndex = 0;



        cbSchool.DataSource = s.Get_List_School();
        cbSchool.DataTextField = "schoolname";
        cbSchool.DataValueField = "school_id";
        cbSchool.DataBind();
        cbSchool.Items.Insert(0, new ListItem("CHOOSE ONE", "ALL"));
        cbSchool.SelectedIndex = 0;


        cbSchool2.DataSource = s.Get_List_School();
        cbSchool2.DataTextField = "schoolname";
        cbSchool2.DataValueField = "school_id";
        cbSchool2.DataBind();
        cbSchool2.Items.Insert(0, new ListItem("CHOOSE ONE", "ALL"));
        cbSchool2.SelectedIndex = 0;



    }
    private void BindGrid()
    {

        MajorSubject d = new MajorSubject();
        // if (Convert.ToInt16(cbSchool.SelectedValue) != -1)
        //{
        DataSet ds = d.Get_List_MajorSubject(cbCity.SelectedValue, cbSchool.SelectedValue);
        gdDisciplines.DataSource = ds;
        gdDisciplines.DataBind();
        gdDisciplines.Columns[1].Visible = false;
        //}

    }
    private void EmptyTextbox()
    {
        txtDisciplines.Text = "";
    }

    protected bool ExistOfMajor(string majorname)
    { 
        string sql="select majorsubject_name from tbl_majorsubject where majorsubject_name='"+majorname+"'";
        SqlDataReader r = SqlHelper.ExecuteReader(Common.strConnect, CommandType.Text, sql);
        if (r.HasRows)
            return true;
        else return false;

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
          if (txtDisciplines.Text != "")
          {
              if (!ExistOfMajor(txtDisciplines.Text))
              {
                  MajorSubject d = new MajorSubject();
                  d.SchoolID = cbSchool.SelectedValue;
                  d.MajorSubjectName = txtDisciplines.Text;
                  d.CityID = cbCity.SelectedValue;
                  d.Insert_Major_Subject();
                  BindGrid();
                  EmptyTextbox();
              }
              else
              {
                  omb.ShowMessage("Major subject name is exist!.", "Info");
              }
            }
          else omb.ShowMessage("Please enter major subject name!.", "Info");
        
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
        //if (txtMajorSubject.Text != "")
          //  m.Update_Major_Subject(nMajorSubjectID, cbCity2.SelectedValue, cbSchool2.SelectedValue, txtMajorSubject.Text);
        //s.Update_School(strSchoolID, txtSchoolID2.Text, txtSchoolName2.Text);
        // string sql="update tbl_schools set school_id ='"+txtSchoolID2.Text +"',schoolname'"+txtSchoolName2.Text+"' 
        BindGrid();
    }
    protected void MajorSubject_Click(object sender, EventArgs e)
    {

        LinkButton lnkButton = sender as LinkButton;
        GridViewRow row = (GridViewRow)lnkButton.NamingContainer;
        nMajorSubjectID = Convert.ToInt16(gdDisciplines.DataKeys[row.RowIndex].Value.ToString());
        string sql = "select * from tbl_majorsubject where id='" + nMajorSubjectID + "'";
        SqlDataReader r = SqlHelper.ExecuteReader(Common.strConnect, CommandType.Text, sql);
        if (r.HasRows)
        {

            while (r.Read())
            {
                cbCity2.SelectedValue = r["city_id"].ToString();
                cbSchool2.SelectedValue = r["school_id"].ToString();
                txtMajorSubject.Text = r["majorsubject_name"].ToString();
            }
        }
        popupEdit.Show();


    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
    protected void gdDisciplines_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void cbCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void cbSchool_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void gdDisciplines_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdDisciplines.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    private void Del_Major_Subject(int id)
    {
        string sql = "delete tbl_majorsubject where id='" + id + "'";

        SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);
    }
    protected void gdDisciplines_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            nMajorSubjectID = Convert.ToInt16(e.CommandArgument);
            Del_Major_Subject(nMajorSubjectID);
            BindGrid();
        }
    }
    protected void gdDisciplines_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Del_Major_Subject(nMajorSubjectID);
    }
    protected void gdDisciplines_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkButtonDelete");
            l.Attributes.Add("onclick", "javascript:return "
                            + "confirm('Are you sure you want to delete this record " + DataBinder.Eval(e.Row.DataItem, "majorsubject_name") + "')");
        }
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
       // FormsAuthentication.SignOut();
     
        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
}
